package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.DisciplinaDAO;
import br.urca.model.Disciplina;

@Transactional
public class JPADisciplinaDAO implements DisciplinaDAO {
	
	private EntityManager manager;
	
	public JPADisciplinaDAO() {
		this(null);
	}
	
	@Inject
	public JPADisciplinaDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Disciplina> todos() {
		return this.manager.createQuery("SELECT d FROM Disciplina d", Disciplina.class)
				.getResultList();
	}

	@Override
	public Disciplina buscarPorId(String codDisciplina) {
		return this.manager.createQuery("SELECT d FROM Disciplina d "
				+ "WHERE d.codDisciplina = :codigo", Disciplina.class)
				.setParameter("codigo", codDisciplina)
				.getSingleResult();
	}

	@Override
	public List<Disciplina> buscarPorDepartamento(String codDepartamento) {
		return this.manager.createQuery("SELECT d FROM Disciplina d "
				+ "WHERE d.departamento.id = :codigo "
				+ "ORDER BY d.nomeDisciplina ", Disciplina.class)
				.setParameter("codigo", codDepartamento)
				.getResultList();
	}

	@Override
	public List<Disciplina> buscarPorNome(String nome) {
		return this.manager.createQuery("SELECT d FROM Disciplina d "
				+ "WHERE d.nomeDisciplina like :nome "
				+ "ORDER BY d.nomeDisciplina ", Disciplina.class)
				.setParameter("nome", nome + "%")
				.getResultList();
	}

}
